<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(awfigq@qq.com)
// +----------------------------------------------------------------------

//供应商家
class GoodsAreaModel extends CommonModel {
	public function createArea($goodsID,$city,$endTime)
	{
        D("GoodsArea")->where("goods_id = $goodsID")->delete();
		$areas = array();
		foreach($_REQUEST['parent_area'] as $area)
		{
			if(!empty($area))
				$areas[] = $area;
		}

		foreach($_REQUEST['child_area'] as $area)
		{
			if(!empty($area))
				$areas[] = $area;
		}

		$areas = array_unique($areas);

        foreach($areas as $area)
		{
			$area_item['goods_id'] = $goodsID;
			$area_item['area'] = $area;
            $area_item['city'] = $city;
			$area_item['end_time'] = $endTime;
			D("GoodsArea")->add($area_item);
		}
	}

	public function deleteArea($ids)
	{
		$condition = array ('goods_id' => array ('in',$ids));
		D("GoodsArea")->where($condition)->delete();
	}

	public function getArea($goodsID,$city = '')
	{
		$goods_areas = array();
		$list = D("GoodsArea")->where("goods_id = $goodsID")->findAll();
		if(empty($list))
			return $goods_areas;

		$city_id = 0;
		if(!empty($city))
		{
			$city_id = D("GroupCity")->where("name = '$city'")->getField('id');
		}

		if($city_id == 0)
			return $goods_areas;

		$areas = D("BusinessCircle")->where("city_id = $city_id")->findAll();
		if(empty($areas))
			return $goods_areas;

		$area_parents = array();
		$area_list = array();

		foreach($areas as $area)
		{
			if($area['parent_id'] == 0)
			{
				$area_parents[$area['id']] = array("name"=>$area['name'],"is_del"=>false);
				$area_list[$area['name']] = array('parent_area'=>$area['name'],'child_area'=>'');
			}
		}

		foreach($areas as $area)
		{
			if($area['parent_id'] > 0)
			{
				$parent = $area_parents[$area['parent_id']];
				$parent_name = $parent['name'];
				if(!$parent['is_del'])
				{
					unset($area_list[$parent_name]);
					$parent['is_del'] = true;
				}

				$area_list[$area['name']] = array('parent_area'=>$parent_name,'child_area'=>$area['name']);
			}
		}

		foreach($list as $item)
		{
			if(isset($area_list[$item['area']]))
			{
				$goods_areas[] = $area_list[$item['area']];
			}
		}

		return $goods_areas;
	}
}
?>